package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.AnonymousThumb;
import com.hpjpw.ido.service.IAnonymousMomentService;
import com.hpjpw.ido.service.IAnonymousThumbService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ido/app/anonymousThumb")
public class IDoAnonymousThumbController extends BaseController {
    @Autowired
    IAnonymousThumbService anonymousThumbService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    IAnonymousMomentService anonymousMomentService;

    /**
     * 查询撕逼墙消息点赞列表
     */
    /*@ApiOperation("查询撕逼墙消息点赞列表，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list")
    public TableDataInfo list(AnonymousThumb anonymousThumb){
        startPage();
        List<AnonymousThumb> list = anonymousThumbService.selectAnonymousThumbList(anonymousThumb);
        return getDataTable(list);
    }*/

    /**
     * 新增撕逼墙消息的点赞
     */
    @ApiOperation("新增撕逼墙消息的点赞，接收momentId(动态id)")
    @PostMapping()
    public AjaxResult addThumb(@RequestBody AnonymousThumb anonymousThumb) {
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(anonymousMomentService.selectAnonymousMomentById(anonymousThumb.getMomentId()))){
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，已点赞，仅根据momentId、thumbedId查询 */
        Long userId = SecurityUtils.getUserId();
        AnonymousThumb thumbCheck = new AnonymousThumb();
        thumbCheck.setMomentId(anonymousThumb.getMomentId());
        thumbCheck.setThumbedId(userId);
        if (anonymousThumbService.selectAnonymousThumbList(thumbCheck).size()!=0){
            return AjaxResult.error("已点赞");
        }
        AnonymousThumb anonymousThumbAdd = new AnonymousThumb();
        anonymousThumbAdd.setMomentId(anonymousThumb.getMomentId());
        anonymousThumbAdd.setThumbedId(userId);
        anonymousThumbAdd.setThumbedName(sysUserService.selectUserById(userId).getNickName());
        anonymousThumbAdd.setTime(new Date());
        anonymousThumbService.insertAnonymousThumb(anonymousThumbAdd);
        return AjaxResult.success();
    }

    /**
     * 删除撕逼墙动态的点赞
     */
    @ApiOperation("删除撕逼墙动态的点赞，接收momentId(动态id)")
    @DeleteMapping()
    public AjaxResult deleteThumb(@RequestBody AnonymousThumb anonymousThumb) {
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(anonymousMomentService.selectAnonymousMomentById(anonymousThumb.getMomentId()))){
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，取消他人点赞，仅根据momentId、thumbedId查询 */
        Long userId = SecurityUtils.getUserId();
        AnonymousThumb thumbCheck = new AnonymousThumb();
        thumbCheck.setMomentId(anonymousThumb.getMomentId());
        thumbCheck.setThumbedId(userId);
        List<AnonymousThumb> anonymousThumbList = anonymousThumbService.selectAnonymousThumbList(thumbCheck);
        if (StringUtils.isNull(anonymousThumbList)||anonymousThumbList.size() == 0){
            return AjaxResult.error("已点赞");
        }
        anonymousThumbService.deleteAnonymousThumbById(anonymousThumbList.get(0).getId());
        return AjaxResult.success();
    }
}
